/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bittorentlike;

import Util.Utils;
import define.Define;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 *
 * @author Xuan Cau
 */
public class Join {
    public Join()
    {
    }
    public void Search(String file_chunk,String file_torrent)
    {
        try
        {
            File file_t = new File(file_torrent);
            FileInputStream is_torrent = new FileInputStream(file_t);
            byte[] torrent = new byte[Define.MAX_CHUNK];
            int len = is_torrent.read(torrent);
            torrent = Utils.SHA1(torrent);
          
            int count = 0;
            boolean found = false;
            File file = new File(file_chunk);
            FileInputStream is_chunk = new FileInputStream(file);
            DataInputStream in = new DataInputStream(is_chunk);
            int len_data = in.readInt();
            while(len_data !=-1)
            {
                byte[] data = new byte[len_data];
                in.read(data);
                if(java.util.Arrays.equals(data,torrent))
                {
                   System.out.println("thuoc chunk :" + count);
                   found = true;
                   break;
                }
                else
                {
                   count ++;
                }
                byte[] endline = new byte[1];
                in.read(endline);
                len_data = in.readInt();
            }
            if(!found)
                System.out.println("khong tim thay");
            
            
        }
        catch(Exception ex)
        {
            
        }
    }
}
